package com.hod.client.backend.database;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.gears.client.database.Database;
import com.google.gwt.gears.client.database.DatabaseException;
import com.google.gwt.gears.client.database.ResultSet;

import java.lang.annotation.Annotation;
import java.util.Date;

public class DBUtil implements com.google.gwt.core.client.UnsafeNativeLong{
   public static native JavaScriptObject createArray() /*-{
    return new Array();
    }-*/;

   public static ResultSet execute(Database db, String sql, JavaScriptObject args) throws DatabaseException
   {
      return getResultSet(nativeExecute(getDBObject(db), sql, args));
   }

   public static native JavaScriptObject getDBObject(Database db) /*-{
    return db;
    }-*/;

   public static ResultSet getResultSet(JavaScriptObject rso)
   {
      return (ResultSet)rso;
   }

   public static native void nativeSetArrayElement(JavaScriptObject array, int index, boolean arg) /*-{
    array[index] = arg;
    }-*/;
   
   public static native void nativeSetArrayElement(JavaScriptObject array, int index, byte arg) /*-{
   array[index] = arg;
   }-*/;

   public static native void nativeSetArrayElement(JavaScriptObject array, int index, char arg) /*-{
   array[index] = arg;
   }-*/;
   
   public static native void nativeSetArrayElement(JavaScriptObject array, int index, Date arg) /*-{
    array[index] = arg;
    }-*/;

   public static native void nativeSetArrayElement(JavaScriptObject array, int index, double arg) /*-{
    array[index] = arg;
    }-*/;

   public static native void nativeSetArrayElement(JavaScriptObject array, int index, float arg) /*-{
    array[index] = arg;
    }-*/;
   public static native void nativeSetArrayElement(JavaScriptObject array, int index, int arg) /*-{
    array[index] = arg;
    }-*/;

   public static native void nativeSetArrayElement(JavaScriptObject array, int index, short arg) /*-{
   array[index] = arg;
   }-*/;
   
   public static native void nativeSetArrayElement(JavaScriptObject array, int index, String arg) /*-{
    array[index] = arg;
    }-*/;
   
  // public static native void nativeSetArrayElement(JavaScriptObject array, int index, long arg) /*-{
  // array[index] = arg;
  // }-*/;
  
   
   /**
    * Native proxy call to the execute method on the JavaScript object.
    * 
    * @param statement the SQL statement to execute
    * @param args arguments to expand, or null if there are none
    * @return the JavaScript object corresponding to a ResultSet
    */
   private static native JavaScriptObject nativeExecute(
		      JavaScriptObject database, String statement, JavaScriptObject args) 
   /*-{
		    if (args == null) {
		      return database.execute(statement);
		    } else {
		      //var newArgs = @com.google.gwt.gears.core.client.impl.GearsImpl::convertToJavaScript([Ljava/lang/String;)(args);
		      return database.execute(statement, args);
		    }
    }-*/;

public Class<? extends Annotation> annotationType() {
	// TODO Auto-generated method stub
	return null;
}

}
